package it.interazionemultimodale.twitterrecognizer.ui;

import java.util.ArrayList;

import it.interazionemultimodale.twitterrecognizer.R;
import it.interazionemultimodale.twitterrecognizer.utility.Methods;
import it.interazionemultimodale.twitterrecognizer.utility.Variables;
import android.app.Activity;
import android.content.Intent;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.os.Bundle;

public class GestureActivity extends Activity
{
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_gesture);
		
		if (Variables.gestureRecognizer == null)
		{
			Variables.gestureRecognizer = GestureLibraries.fromRawResource(this, R.raw.gestures);
		
			if (!Variables.gestureRecognizer.load())
				finish();
		}
			
		System.out.println(Variables.gestureRecognizer);
		
		GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
		gestures.addOnGesturePerformedListener(new GestureOverlayView.OnGesturePerformedListener()
		{
			@Override
			public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture)
			{
				System.out.println("Gesture listener called");
				ArrayList<Prediction> predictions = Variables.gestureRecognizer.recognize(gesture);
				
				if (predictions.size() > 0 && predictions.get(0).score > 1.0)
				{
					String result = predictions.get(0).name;
					returnResult(Integer.parseInt(result));
					System.out.println("Gesture riconosciuta");
				}
				else
					Methods.showToast(GestureActivity.this, "Gesture non riconosciuta");
			}
		});
	}
	
	public void returnResult(int value)
	{
		Intent returnIntent = new Intent();
		returnIntent.putExtra("tweets_number", value);
		this.setResult(Activity.RESULT_OK, returnIntent);
		this.finish();
	}
}
